package com.kingyun.gpsinspection.purificationservice.services.tdm.outerservice.impl;

import com.kingyun.gpsinspection.purificationservice.facade.model.jsm.EmployeeVo;
import com.kingyun.gpsinspection.purificationservice.facade.model.jsm.RedisValue;
import com.kingyun.gpsinspection.purificationservice.dao.mapper.tdm.MaterialsManageMapper;
import com.kingyun.gpsinspection.purificationservice.dao.mapper.tdm.WorkPermitMapper;
import com.kingyun.gpsinspection.purificationservice.facade.model.tdm.*;
import com.kingyun.gpsinspection.purificationservice.facade.service.tdm.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

/**
 * Created by liyi on 2017/2/23.
 * 任务流程对外的接口
 */
public class OutTaskFlowServiceImpl implements OutTaskFlowService {
    public Logger logger = LoggerFactory.getLogger(this.getClass());
    @Autowired
    public MaterialsManageService materialsManageService;
    @Autowired
    public WorkPermitService workPermitService;
    @Autowired
    public TerritorialSupervisionService territorialSupervisionService;
    @Autowired
    public WorkOrderService workOrderService;
    @Autowired
    public MaterialsManageMapper materialsManageMapper;
    @Autowired
    private WorkPermitMapper workPermitMapper;

    /**
     * 物资申请任务：保存物料管理信息并发起流程
     *
     * @param materialsManage
     * @param employeeVo
     * @return
     */
    @Override
    public MaterialsManageVo saveMaterialsManage(RedisValue redisValue, EmployeeVo employeeVo, MaterialsManageVo materialsManage, String uploadFiles, String deletedFiles) {
        MaterialsManageVo materials = null;
        try {
            materials = materialsManageService.saveMaterialsManage(redisValue, employeeVo, materialsManage, uploadFiles, deletedFiles);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        return materials;
    }

    /**
     * 根据主键查询物资申请任务信息
     *
     * @param id
     * @return
     */
    @Override
    public MaterialsManageVo queryMaterialsManageVoById(String id) throws Exception {
        MaterialsManage materialsManage = materialsManageMapper.selectByPrimaryKey(id);
        // 根据任务流程ID查询物资信息
        MaterialsManageVo materialsManageVo = materialsManageService.findMaterialsManage(materialsManage.getFlowId());
        return materialsManageVo;
    }

    /**
     * 作业许可任务：保存作业许可并发起流程
     *
     * @param employeeVo
     * @param workPermit
     * @param uploadFiles
     * @param deletedFiles
     * @return
     * @throws Exception
     */
    @Override
    public WorkPermit saveWorkPermit(RedisValue redisValue, EmployeeVo employeeVo, WorkPermit workPermit, String uploadFiles, String deletedFiles, List<WorkOrderVo> workOrderList) {
        WorkPermit permit = null;
        try {
            permit = workPermitService.saveWorkPermit(redisValue, employeeVo, workPermit, uploadFiles, deletedFiles, workOrderList);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        return permit;
    }

    /**
     * 根据主键查询物作业许可任务信息
     *
     * @param id
     * @return
     */
    @Override
    public WorkPermitVo queryWorkPermitVoById(String id) throws Exception {
        WorkPermit workPermit = workPermitMapper.selectByPrimaryKey(id);
        // 根据任务ID查找作业许可信息
        WorkPermitVo workPermitVo = workPermitService.findWorkPermit(workPermit.getFlowId());
        return workPermitVo;
    }

    /**
     * @param redisValue
     * @param employeeVo
     * @param territorialSupervision
     * @param uploadFiles
     * @param deletedFiles
     * @param workOrderList
     * @return 杨春生需要接收返回的对象取到新对象的主键ID
     */
    @Override
    public TerritorialSupervision saveTerritorialSupervision(RedisValue redisValue, EmployeeVo employeeVo, TerritorialSupervision territorialSupervision, String uploadFiles, String deletedFiles, List<WorkOrderVo> workOrderList) {
        TerritorialSupervision ts = null;
        try {
            TaskRelation relation = new TaskRelation();
            ts = territorialSupervisionService.save(redisValue, employeeVo, territorialSupervision, uploadFiles, deletedFiles, workOrderList, relation);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }

        return ts;
    }

    /**
     * 根据主键查询属地监督包装类
     *
     * @param id
     * @return
     */
    @Override
    public TerritorialSupervisionVo queryTerritorialSupervisionVoById(String id) throws Exception {
        return territorialSupervisionService.queryVoById(id);
    }

    /**
     * 根据任务ID查询任务所属工单
     *
     * @param taskId
     * @return
     */
    public List<WorkOrderVo> findListByTaskId(String taskId) throws Exception {
        return workOrderService.findVoListByTaskId(taskId);
    }
}
